package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.SaleListGoodsDao;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.SaleList;
import com.atguigu.jxc.entity.SaleListGoods;
import com.atguigu.jxc.service.GoodsService;
import com.atguigu.jxc.service.SaleListGoodsService;
import com.atguigu.jxc.service.SaleListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * description
 *
 * @author SZ
 * @date 2022/9/26/8:35
 */
@Service
public class SaleListGoodsServiceImpl implements SaleListGoodsService {
    @Autowired
    private SaleListGoodsDao saleListGoodsDao;
    @Autowired
    private SaleListService saleListService;
    @Autowired
    private GoodsService goodsService;

    @Override
    @Transactional
    public ServiceVO save(SaleList saleList, List<SaleListGoods> saleListGoodsList) {
        //1-往t_sale_list添加一条记录,后面需要使用该表的主键
        saleListService.save (saleList);

        //2-往t_sale_list_goods添加若干记录
        for (SaleListGoods saleListGoods : saleListGoodsList) {
            saleListGoods.setSaleListId (saleList.getSaleListId ());
            saleListGoodsDao.save (saleListGoods);
            //3-同时往t_goods削减库存，
            Integer goodsId = saleListGoods.getGoodsId ();
            Integer goodsNum = saleListGoods.getGoodsNum ();
            goodsService.minusQuantity (goodsNum, goodsId);//记得做条件判断
        }

        return new ServiceVO (SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

    @Override
    public List<SaleListGoods> getGoodsList(Integer saleListId) {
        if (saleListId != null) {
            List<SaleListGoods> saleListGoodsList = saleListGoodsDao.selectGoodsList (saleListId);
            return saleListGoodsList;
        }
        return null;
    }
}
